package _10_binary_tree.symmetric;

import _10_binary_tree.TreeNode;
import org.junit.Test;

/**
 * @author: mornd
 * @dateTime: 2023/6/11 - 22:21
 * 给定一个二叉树，判断其是否对称，从中间对折每个节点的值能对应上
 */
public class MyTest {
    @Test
    public void test() {
        /*

                    1
                  /   \
                2       2
               / \     / \
              3   4   4   3
             / \ / \ / \ / \
            5  6 7 8 8 7 6  5
         */
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2, new TreeNode(3, new TreeNode(5), new TreeNode(6)), new TreeNode(4, new TreeNode(7), new TreeNode(8)));
        root.right = new TreeNode(2, new TreeNode(4, new TreeNode(8), new TreeNode(7)), new TreeNode(3, new TreeNode(6), new TreeNode(5)));
        System.out.println(isSymmetric(root));
        System.out.println(root.printTreeStructure());
    }

    private boolean isSymmetric(TreeNode root) {
        return check(root.left, root.right);
    }

    private boolean check(TreeNode left, TreeNode right) {
        if(left == null && right == null) {
            return true;
        }
        if(left == null || right == null || left.val != right.val) {
            return false;
        }
        // left 的 左子与 right 的右子比较
        return check(left.left, right.right) && check(left.right, right.left);
    }
}
